﻿using B.S.Traceability.Myproject.ErrorCode;
using B.S.Traceability.MyWrite.Api.Appicant.Command.InformationCommand;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.Traceability.MyWrite.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class InformationController : ControllerBase
    {
        private readonly IMediator mediator;

        public InformationController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 添加商品管理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<string>> Addinformation(AddInformationCommand request)
        {
           return await mediator.Send(request);


        }

        //[HttpPost]
        //public async Task<IActionResult> UploadImage(IFormFile imageFile)
        //{
        //     // 1. 空文件校验
        //    if (imageFile == null || imageFile.Length == 0)
        //        return BadRequest("请选择有效文件");

        //    // 2. 文件类型校验
        //    var allowedExtensions = new[] { ".jpg", ".jpeg", ".png", ".gif" };
        //    var fileExtension = Path.GetExtension(imageFile.FileName).ToLower();
        //    if (!allowedExtensions.Contains(fileExtension))
        //        return BadRequest("仅支持图片格式：JPG/PNG/GIF");

        //    // 3. 文件大小校验
        //    if (imageFile.Length > 20 * 1024 * 1024) // 5MB
        //        return BadRequest("文件大小不能超过20MB");

        //    // 4. 生成唯一文件名
        //    var fileName = Guid.NewGuid().ToString() + fileExtension;
        //    var uploadsFolder = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/uploads");
        //    // 如果目录不存在则创建
        //    Directory.CreateDirectory(uploadsFolder); // 新增此行
        //    var savePath = Path.Combine(uploadsFolder, fileName);
        //    // 5. 保存文件
        //    using (var stream = new FileStream(savePath, FileMode.Create))
        //    {
        //        await imageFile.CopyToAsync(stream);
        //    }

        //    // 6. 返回结果
        //    var imageUrl = $"/uploads/{fileName}";
        //    return Ok(new { imageUrl });
        //}
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UploadImage(IFormFile file)
        {
            try
            {
                var str = Guid.NewGuid();
                var p = Path.Combine("img", str + file.FileName);
                using (var stearam = new FileStream(p, FileMode.Create))
                {
                    file.CopyTo(stearam);
                }
                return Ok(new
                {
                    code = 3,
                    data = "https://localhost:7106/img/" + str + file.FileName,
                });
            }
            catch (Exception)
            {

                throw;
            }
        }


    }
}
